package Interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

//@Configuration
//public class NewlyBuildInterceptor implements HandlerInterceptor {
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//        HttpServletRequest request1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        request1=request;
//        StringBuffer requestURL = request1.getRequestURL();
//        String s=new String(requestURL);
//        System.out.println("访问地址为："+s);
//        byte[] bytes = StreamUtils.copyToByteArray(request1.getInputStream());
//        String body = new String(bytes, request.getCharacterEncoding());
//        System.out.println("请求实体："+body);
//        return true;
//    }
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
//        System.out.println("==============postHandle");
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        System.out.println("==============afterCompletion");
//    }
//}
@WebFilter(urlPatterns = "/*", filterName = "logFilter2")
@Component
@Slf4j
public class NewlyBuildInterceptor implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest requestWrapper=null;
        HttpServletRequest req=(HttpServletRequest)servletRequest;
        ServletInputStream inputStream = req.getInputStream();

        String requestURI = req.getRequestURI();
        String method = req.getMethod();
        String remoteAddr = req.getRemoteAddr();
        if("POST".equals(method)){
            requestWrapper = new HttpServletRequestWrappe(req);
            String body = Interceptor.HttpHelper.getBodyString(requestWrapper);

            log.info("拦截器请求参数为"+body);
            log.info("拦截器拦截地址为"+requestURI);
            log.info("拦截器获取的ip为"+remoteAddr);
        }
        if(null==requestWrapper){
            log.info("aaaaaaaaaaaaaaaaaaa");
            if(remoteAddr!=null&&remoteAddr.equals("0:0:0:0:0:0:0:1.0")){
                req.getRequestDispatcher("/logininterceptor").forward(servletRequest,servletResponse);//跳转自己指定的url
            }else {
                filterChain.doFilter(servletRequest,servletResponse);
            }

        }else {
            log.info("bbbbbbbbbbbbbbbb");
            if(remoteAddr!=null&&remoteAddr.equals("0:0:0:0:0:0:0:1")){
//                ResponseWrapper responseWrapper=new ResponseWrapper((HttpServletResponse)servletResponse);
                filterChain.doFilter(requestWrapper,servletResponse);
//                byte[] content = responseWrapper.getContent();//获取response返回值
//                if(content.length>0){//判断返回值是否有值
//                    String str=new String(content,"UTF-8");
//                    log.info("返回值信息为"+str);
//                }
//                req.getRequestDispatcher("/logininterceptor").forward(servletRequest,servletResponse);//拦截并跳转自己指定的url
//                req.getRequestDispatcher("/testinterface").forward(servletRequest,servletResponse);
            }else{
                filterChain.doFilter(requestWrapper,servletResponse);
            }
        }
    }
}